﻿using System.Collections.Generic;
using Splendor_Server.script.net;

namespace Splendor_Server.script.logic.room
{
    public static class RoomManager
    {
        //下一个id
        private static int nextId = 0;

        //房间列表
        private static Dictionary<int, Room> rooms = new Dictionary<int, Room>();

        //创建房间
        public static Room AddRoom(int isPrivate, int point, int rules)
        {
            Room room = new Room(isPrivate, point, rules);
            rooms.Add(room.id, room);
            return room;
        }

        //删除房间
        public static void RemoveRoom(int id)
        {
            rooms.Remove(id);
            MsgRemoveRoom msg = new MsgRemoveRoom() {rId = id};
            NetManager.SendAll(msg);
        }

        //获取房间
        public static Room GetRoom(int id)
        {
            return rooms.ContainsKey(id) ? rooms[id] : null;
        }

        public static RoomInfo[] ToRoomInfoList()
        {
            RoomInfo[] roomInfos = new RoomInfo[rooms.Count];

            int i = 0;
            foreach (Room room in rooms.Values)
            {
                roomInfos[i] = room.ToRoomInfo();
                i++;
            }

            return roomInfos;
        }

        public static void Update()
        {
            foreach (Room room in rooms.Values)
                room.Update();
        }

        public static int GetNextRoomId()
        {
            ++nextId;
            return nextId;
        }
    }
}